package cc.customer.bigEmport.services.imp;

import cc.customer.ServicesUtils.NoticeUtils;
import cc.customer.bigEmport.damain.*;
import cc.customer.bigEmport.dao.*;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.bigEmport.services.BigEmpServices;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.FebsUtil;
import cc.customer.common.utils.Utils;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.cusDynamic.services.CusDynamicService;
import cc.customer.rabbit.domain.CconvNotice;
import cc.customer.sendMsg.SendMagRecevicer;
import cc.customer.system.dao.UserMapper;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.utils.AutonCusInfo;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

@Service("bigEmpServices")
@Transactional(propagation = Propagation.SUPPORTS,  rollbackFor = Exception.class)
public class BigEmpServicesImp extends BaseService<BigEmp> implements BigEmpServices {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BigEmpMapper bigEmpMapper;
    @Autowired
    private EpRecordsMapper epRecordsMapper;
    @Autowired
    private EprRecordsMapper eprRecordsMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private IpRecordsMapper ipRecordsMapper;
    @Autowired
    private  BigIntenMapper bigIntenMapper;
    @Autowired
    private CusDynamicService cusDynamicService;
    @Autowired
    private SendMagRecevicer sendMagRecevicer;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public int batchInsert(List<BigEmp> list, EpRecords records) {
        List<BigEmp> all = new ArrayList<>();
        List<EprRecords> allChongfu = new ArrayList<>();
        int num = 0;
        for (BigEmp big:list){
            //验证 手机号 是不是已经存在了
            BigEmp bigIshava = findBigEmpByMobile(big.getBpMobile());
            log.error(big.toString());
            if (bigIshava == null){
                all.add(big);
                if(all.size()>300){
                    int num1 = bigEmpMapper.insertList(all);
                    num +=num1;
                    all.clear();
                }
            }else{
                EprRecords epr  = new EprRecords();
                epr.setcEprPhone(big.getBpMobile());
                epr.setcEprUser(big.getBpName());
                epr.setcEprTime(new Date());
                epr.setcEprBatch(big.getBpBatch());
                epr.setcEprBacthUuid(records.getEpUuid());
                allChongfu.add(epr);
            }
        }
        //最后插入结束
        int numlast = 0;
        if(!all.isEmpty()){
            numlast = bigEmpMapper.insertList(all);
        }
        /**重复数据
         * **/
        //验证数据库有的数据 执行插入到其他的表里面
        if (!allChongfu.isEmpty()){
            List<EprRecords> Chongfu = new ArrayList<>();
            for (EprRecords big:allChongfu){
                //分批插入
                big.setcEprReson("根据手机号查询,当前大资源已经存在资源");
                Chongfu.add(big);
                if(Chongfu.size()>300){
                    eprRecordsMapper.insertList(Chongfu);
                    Chongfu.clear();
                }
            }
            if (!Chongfu.isEmpty()){
                eprRecordsMapper.insertList(Chongfu);
            }
        }
        records.setEpFNum(allChongfu.size()+"");
        records.setEpBalanceNum("0");
        records.setEpBalanceOver("0");
        num+=numlast;
        //批量插入完成 后执行批量插入的 记录
        epRecordsMapper.insert(records);
        return num;
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public int insertRecord(List<EprRecords> allChongfu,EpRecords records){
        try {
            //验证数据库有的数据 执行插入到其他的表里面
            if (!allChongfu.isEmpty()){
                List<EprRecords> Chongfu = new ArrayList<>();
                for (EprRecords big:allChongfu){
                    //分批插入
                    Chongfu.add(big);
                    if(Chongfu.size()>300){
                        eprRecordsMapper.insertList(Chongfu);
                        Chongfu.clear();
                    }
                }
                if (!Chongfu.isEmpty()){
                    eprRecordsMapper.insertList(Chongfu);
                }
            }
            records.setEpFNum(allChongfu.size()+"");
            records.setEpBalanceNum("0");
            records.setEpBalanceOver("0");
            //批量插入完成 后执行批量插入的 记录
            epRecordsMapper.insert(records);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("生成导入大资源信息失败");
        }
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Async
    public Future<Integer> batchInsertOne(List<BigEmp> list, EpRecords records) throws Exception{

        int num = 0;
        try {
            List<BigEmp> all = new ArrayList<>();
            for (BigEmp big:list){
                big.setBpGTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
                all.add(big);
                if(all.size()>300){
                    int numq = bigEmpMapper.insertList(all);
                    num+=numq;
                    all.clear();
                }
            }
            if(!all.isEmpty()){
                int numla = bigEmpMapper.insertList(all);
                num += numla;
            }
        }catch (Exception e){
            throw  new  RuntimeException("批次导入失败："+e.getMessage());
        }
        return new AsyncResult<Integer>(num);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Async
    public Future<Integer> batchInsertTwo(List<BigEmp> list, EpRecords records) {
        int num = 0;
        try {
            List<BigEmp> all = new ArrayList<>();
            for (BigEmp big:list){
                big.setBpGTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
                all.add(big);
                if(all.size()>300){
                    int numq = bigEmpMapper.insertList(all);
                    num+=numq;
                    all.clear();
                }
            }
            if(!all.isEmpty()){
                int numla = bigEmpMapper.insertList(all);
                num += numla;
            }
        }catch (Exception e){
            throw  new  RuntimeException("批次导入失败："+e.getMessage());
        }
        return new AsyncResult<Integer>(num);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Async
    public Future<Integer> batchInsertThree(List<BigEmp> list, EpRecords records) throws Exception {
        int num = 0;
        try {
            List<BigEmp> all = new ArrayList<>();
            for (BigEmp big:list){
                big.setBpGTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
                all.add(big);
                if(all.size()>300){
                    int numq = bigEmpMapper.insertList(all);
                    num+=numq;
                    all.clear();
                }
            }
            if(!all.isEmpty()){
                int numla = bigEmpMapper.insertList(all);
                num += numla;
            }
        }catch (Exception e){
            throw  new  RuntimeException("批次导入失败："+e.getMessage());
        }
        return new AsyncResult<Integer>(num);
    }

    @Override
    public List<BigEmp> findAllBalanceByUuidAndGroup(String uuid, String group) {
        try {
            Example example = new Example(BigEmp.class);
            Example.Criteria criteria = example.createCriteria();
            if (!StringUtils.isEmpty(group)){
                List<String> allGroup = Arrays.asList(group.split(","));
                criteria.andIn("bpGroud",allGroup);
            }
            criteria.andEqualTo("bpUuid",uuid);
            List<BigEmp> list =  this.selectByExample(example);
            return list;
        }catch (Exception e){
            log.error("查询可结算的客户失败:"+e.getMessage());
        }
        return new ArrayList<>();
    }
    @Override
    public List<BigEmp> balanceEmpNoGroup(String uuid) {
        try {
            Example example = new Example(BigEmp.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIsNull("bpGroud").orEqualTo("bpGroud","");
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.andEqualTo("bpUuid",uuid);
            example.and(criteria1);
            List<BigEmp> list =  this.selectByExample(example);
            return list;
        }catch (Exception e){
            log.error("查询可结算的客户失败:"+e.getMessage());
        }
        return new ArrayList<>();
    }

    @Override
    public List<BigEmp> findMyAllBigEmp(BigEmp emp, MyUser user) {
        List<BigEmp> list = new ArrayList<>();
        try {
            Boolean ishavaAllPre = false;
            List<String> listall = user.getAllAuthority();
            if (listall.contains(Constant.AMDIN_AUTORITY_big)){
                ishavaAllPre= true;
            }
            Example example = new Example(BigEmp.class);
            Example.Criteria criteria = example.createCriteria();
            /***首先获取我的用户***/
            if (!ishavaAllPre){
                Example.Criteria criteria1 = example.createCriteria();
                String  userUuid = user.getUserUuid();
                criteria1.andLike("bpDistribute","%"+userUuid+"%")
                        .orLike("bpMyCustomer","%"+userUuid+"%");
                example.and(criteria1);
            }
            criteria.andEqualTo("bpIsdelete","0");
            /**加入查询条件**/
            //排序条件
            example.setOrderByClause(" bp_createtime desc");
            /**标签**/
            /**最新更新的时间**/
            list = this.selectByExample(example);
        }catch (Exception e){
            log.error(""+e.getMessage());
        }
        return list;
    }

    @Override
    @Transactional
    public int batchUpdateInten(List<BigEmp> list,String user) {
        List<BigEmp> listupdate = new ArrayList<>();
        /**设置更新的优先级 **/
        for (BigEmp emp:list){
            BigEmp bigEmp = findBigEmpByMobile(emp.getBpMobile());
            if (bigEmp!=null){
                String gpup = bigEmp.getBpGroud();
                if (!StringUtils.isEmpty(gpup)){
                    int num = emp.getBpGroud().compareTo(gpup);
                    if (num < 0){
                        emp.setBpGroud(emp.getBpGroud());
                        listupdate.add(emp);
                    }
                }else{
                    listupdate.add(emp);
                }
            }
        }
        int num = 0;
        log.info(listupdate.size()+"");
        if(!listupdate.isEmpty()){
            //批量更新
             num = bigEmpMapper.updateBatchBigEmp(listupdate);
            /**增加更新意向得记录**/
            BigInten bigInten = new BigInten();
            bigInten.setcBginNum(listupdate.size());
            bigInten.setcBginUser(user);
            bigInten.setcBginTime(new Date());
            bigInten.setcBginFnum(list.size()-listupdate.size());
            bigIntenMapper.insert(bigInten);
        }
        return num;
    }

    @Override
    public List<BigEmp> findAllBigEmp(BigEmp bigEmp, QueryRequest request,String isBig,MyUser user,List<String> list1) {
        try {
            Example example = new Example(BigEmp.class);
            Example.Criteria criteria = example.createCriteria();
            /**查询批次**/
            String batch = bigEmp.getBpBatch();
            if(!StringUtils.isEmpty(batch)){
                criteria.andLike("bpBatch","%"+batch+"%");
            }
            /**查询专员**/
            String bpDirector = bigEmp.getBpDirector();
            if (!StringUtils.isEmpty(bpDirector)){
                criteria.andLike("bpDirector","%"+bpDirector+"%");
            }
            /**筛选部门*/
            if(list1.size()>0){
                criteria.andIn("bpDistribute",list1);
            }

            /**
             * 查询高校条件
             * */
            String collage = bigEmp.getBpColleges();
            if (!StringUtils.isEmpty(collage)){
                criteria.andLike("bpColleges","%"+collage+"%");
                /**查询学院*/
                String bpAcademy = bigEmp.getBpAcademy();
                if (!StringUtils.isEmpty(bpAcademy)){
                    criteria.andLike("bpAcademy","%"+bpAcademy+"%");
                    /**
                     * 查询专业
                     * **/
                    String bpMajor = bigEmp.getBpMajor();
                    if (!StringUtils.isEmpty(bpMajor)){
                        criteria.andLike("bpMajor","%"+bpMajor+"%");
                    }
                }
            }
            /**意向等级**/
            String bpGroud = bigEmp.getBpGroud();
            if (!StringUtils.isEmpty(bpGroud)){
                try {
                    List<String> list = Arrays.asList(bpGroud.split(","));
                    criteria.andIn("bpGroud",list);
                }catch (Exception e){
                    log.error("查询大资源客户的 意向等级 失败："+e.getMessage());
                }
            }
            /**导出的次数
             * **/
            String bpEpNum = bigEmp.getBpEpNum();
            if (!StringUtils.isEmpty(bpEpNum)){
                try {
                    List<String> list = Arrays.asList(bpEpNum.split(","));
                    criteria.andIn("bpEpNum",list);
                }catch (Exception e){
                    log.error("查询大资源客户的 导出数目 失败："+e.getMessage());
                }
            }
            /**资源类型**/
            String bpResType = bigEmp.getBpResType();
            if(!StringUtils.isEmpty(bpResType)){
                criteria.andCondition("bp_res_type = ",bpResType);
            }else{
                List<String> list = new ArrayList<>(Arrays.asList(Constant.BIGEMP_RESOURCE_TYPE));
                criteria.andIn("bpResType",list);
            }
            /**
             * 意向项目**/
            String bpInteAipro = bigEmp.getBpInteAipro();
            if(!StringUtils.isEmpty(bpInteAipro)){
                criteria.andLike("bpInteAipro","%"+bpInteAipro+"%");//andCondition("bp_inte_aipro = ",bpInteAipro);
            }
            /**
             * 意向项目**/
            String bpInteProject = bigEmp.getBpInteProject();
            if(!StringUtils.isEmpty(bpInteProject)){
                criteria.andCondition("bp_inte_project = ",bpInteProject);
            }
            /**来源渠道**/
            String bpSOurChanel = bigEmp.getBpSourChanel();
            if(!StringUtils.isEmpty(bpSOurChanel)){
                criteria.andCondition("bp_sour_chanel = ",bpSOurChanel);
            }
            /**客户新增时间**/
            String starttime=  bigEmp.getStarttime();
            String endtime = bigEmp.getEndtime();
            if (!StringUtils.isAnyEmpty(starttime,endtime)){
                criteria.andBetween("bpCreatetime",starttime,endtime+" 23:59:59");
            }else if (!StringUtils.isEmpty(starttime)){
                criteria.andCondition("bpCreatetime = ",starttime);
            }
            /**客户的状态**/
            String  bpDistribute = bigEmp.getBpDistribute();
            if (!StringUtils.isEmpty(bpDistribute)){
                //已分配
                if ("distrut".equals(bpDistribute)){
                    criteria.andIsNotNull("bpDistribute");
                }else{
                    //未分配
                    criteria.andIsNull("bpDistribute");
                }
            }
            /**查询是否已经分配 或者是 有意向 定向于意向客户资源 模块**/
            if (!StringUtils.isEmpty(isBig)){
                Example.Criteria criteria1 = example.createCriteria();
                Example.Criteria criteria2 = example.createCriteria();
                criteria1.andNotEqualTo("bpGroud","").andIsNotNull("bpGroud");
                //criteria2.andIsNull("bpDistribute").orEqualTo("bpDistribute","");
                example.and(criteria1);
                //example.and(criteria2);
            }

            /**筛选跟进人*/
            if(!StringUtils.isEmpty(bigEmp.getBpMyCustomerName())){
                criteria.andEqualTo("bpDistribute",bigEmp.getBpMyCustomerName());
            }

            /**学历层次*/
            if(!StringUtils.isEmpty(bigEmp.getBpEducation())){
                criteria.andEqualTo("bpEducation",bigEmp.getBpEducation());
            }

            criteria.andEqualTo("bpIsdelete","0");


            /**加入查询条件**/
            //排序条件
            example.setOrderByClause(" bp_createtime desc ");
            List<BigEmp> bigEmpList = this.selectByExample(example);
            List<MyUser> uslist = userMapper.selectAllUser();
            for (BigEmp bigEmp1:bigEmpList){
                AutonCusInfo.parseBigEmp(bigEmp1,user);
                for(MyUser user1:uslist){
                    if(bigEmp1.getBpDistribute()!=null&&!bigEmp1.getBpDistribute().isEmpty()){
                        if(bigEmp1.getBpDistribute().equals(user1.getUserUuid())){
                            bigEmp1.setBpDistribute(user1.getNickname());
                            bigEmp1.setBpDeptName(user1.getDeptName());
                        }
                    }

                }
            }


            return bigEmpList;
        }catch (Exception e){
            e.printStackTrace();
            log.error("获取大名单资源失败"+e.getMessage());
            return new ArrayList<>();
        }
    }
    @Override
    public BigEmp findBigEmpByMobile(String Mobile) {
        Example example = new Example(BigEmp.class);
        example.createCriteria().andCondition("bp_mobile=",Mobile).andEqualTo("bpIsdelete","0");
        List<BigEmp> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }
    @Override
    public List<BigEmp> findBigEmpByImportBatch(String importBatch) {
        Example example = new Example(BigEmp.class);
        example.createCriteria().andCondition("bp_batch=",importBatch).andEqualTo("bpIsdelete","0");
        List<BigEmp> list = this.selectByExample(example);
        return list;
    }

    @Override
    public List<BigEmp> findBigEmpByImportUuid(String importUuid,String conditon) {
        Example example = new Example(BigEmp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition("bp_uuid=",importUuid).andEqualTo("bpIsdelete","0");
        Example.Criteria criteria1 = example.createCriteria();
        if (!StringUtils.isEmpty(conditon)){
            criteria1.orLike("bpName","%"+conditon+"%").orLike("bpMobile","%"+conditon+"%");
        }
        example.and(criteria1);
        List<BigEmp> list = this.selectByExample(example);
        return list;
    }

    @Override
    public ResponseBo findAllBigEmpInfoById(String keyid,MyUser user) {
        try {
            BigEmp igEmpbig = bigEmpMapper.selectCustomerDetail(keyid);
            BigEmp emp = AutonCusInfo.parseBigEmp(igEmpbig,user);
            return  ResponseBo.ok(emp);
        }catch (Exception e){
            log.error("获取客户资料失败 :"+e.getMessage());
        }
        return ResponseBo.error("获取客户资料失败");
    }
    @Override
    public ResponseBo AppfindAllBigEmpInfoById(String keyid,MyUser user) {
        try {
            BigEmp igEmpbig = bigEmpMapper.selectCustomerDetail(keyid);
            return  ResponseBo.ok(igEmpbig);
        }catch (Exception e){
            log.error("获取客户资料失败 :"+e.getMessage());
        }
        return ResponseBo.error("获取客户资料失败");
    }


    @Override
    public ResponseBo selectBigEmpLabel(String keyid) {
        try {
            BigEmp igEmpbig = bigEmpMapper.selectBigEmpLabel(keyid);
            return  ResponseBo.ok(igEmpbig);
        }catch (Exception e){
            log.error("获取客户资料失败 :"+e.getMessage());
        }
        return ResponseBo.error("获取客户资料失败");

    }
    @Override
    public ResponseBo batchUpdateBigEmp(String ids) {
        try {
            String idsArray [] = ids.split(",");
            List<String> list = new ArrayList<>();
            for (String id:idsArray){ list.add(id); }
            BigEmp bigEmp = new BigEmp();
            bigEmp.setBpIsdelete("1");
            int num  = this.batchUpdate(list,"bpId",BigEmp.class,bigEmp);
            if (num<=0){
                return ResponseBo.error("删除客户得信息失败");
            }
        }catch (Exception e){
            log.error("删除客户失败:"+e.getMessage());
            return ResponseBo.error("删除客户得信息失败");
        }
        return ResponseBo.ok("删除成功");
    }

    /**批量更新客户的信息**/
    //TODO("批量分配用户的时候增加系统通知")
    @Override
    @Transactional
    public ResponseBo batchUpdateBykeyId(BigEmp bigEmp, String ids,String userid) throws RuntimeException {
        try {
            List<String> list = Arrays.asList(ids.split(","));
            int num  = this.batchUpdate(list,"bpId",BigEmp.class,bigEmp);
            if (num > 0){
                /**增加操作记录**/
                cusDynamicService.batchInsertBigEmpDisture(list,bigEmp);

                Example example = new Example(BigEmp.class);
                example.createCriteria().andIn("bpId",list);
                List<BigEmp> getListEmp = this.selectByExample(example);
                List<String> listUuis = new ArrayList<>();
                List<String> listname = new ArrayList<>();
                List<String> listods = new ArrayList<>();
                for(BigEmp bigEmp1:getListEmp){
                    listUuis.add(bigEmp1.getBpRUuid());
                    listname.add(bigEmp1.getBpName());
                    listods.add(bigEmp1.getBpId()+"");
                }
                CconvNotice notice = NoticeUtils.bigEmpBatchNotice(bigEmp.getBpDisPerson(),list.size(),listname,listUuis,listods);
                String rid = bigEmp.getBpDistribute();
                String username = userService.findUserByUuids(rid).getUsername();
                notice.setcConvUsers(username+","+userid);
                sendMagRecevicer.processUsers(notice);
                return ResponseBo.ok("更新客户的信息成功");
            }
        }catch (Exception e){
            log.error("更新客户失败:"+e.getMessage());
            throw  new RuntimeException();
        }
        return ResponseBo.error("更新客户的信息失败");
    }

    @Override
    @Transactional
    public ResponseBo updateBigEmpInfoByBpId(BigEmp emp,MyUser user) {
        try {
            /**如果更新得是手机号，则验证手机号 是不是存在 如果存在 则不能更新**/
            String mobilePhone = emp.getBpMobile();
            if (!StringUtils.isEmpty(mobilePhone)){
                //验证手机号
                Boolean ble = FebsUtil.isPhoneNo(mobilePhone);
                if (!ble){
                    return ResponseBo.error("当前手机号不正确，不允许更新");
                }
                BigEmp isHave = findBigEmpByMobile(mobilePhone);
                if (isHave !=null){
                    if(!isHave.getBpId().equals(emp.getBpId())){
                        return ResponseBo.error("当前手机号已经存在，不允许更新");
                    }
                }
            }
            //修改学历
            if(!StringUtils.isEmpty(emp.getBpEducation())){
                String edu = AutonCusInfo.parseBigExmEducation(emp.getBpEducation());
                emp.setBpEducation(edu);
            }
            int num = this.updateNotNull(emp);
            if (num <= 0){
                return ResponseBo.error("修改失败,修改条目:"+num);
            }
        }catch (Exception e){
            log.error("修改客户得信息失败:"+e.getMessage());
            return ResponseBo.error("修改客户得信息失败:"+e.getMessage());
        }
        if (!StringUtils.isEmpty(emp.getBpStage())){
            List<String> list = new ArrayList<>();
            list.add(emp.getBpId()+"");
            CusDynamic cus = new CusDynamic();
            cus.setDyCreator(user.getUserUuid());
            cus.setDyType(Constant.CUSTOMER_RECORD_STATAGES);
            cus.setDySourceType("3");
            String stta = AutonCusInfo.parseCustomerStage(emp.getBpStage());
            cus.setDyContent(user.getNickname()+" 更改了客户阶段: "+stta);
            cusDynamicService.inserDynamic(list,cus);
        }
        return ResponseBo.ok("修改客户资料成功");
    }

    @Override
    @Transactional
    public ResponseBo addBigEmp(BigEmp emp) {
        try {
            String uuid = Utils.getUUID();
            emp.setBpUuid(uuid);
            emp.setBpCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            this.save(emp);
        }catch (Exception e){
            log.error("保存失败"+e.getMessage());
            return ResponseBo.error("新增客户失败");
        }
        return ResponseBo.ok("新增客户成功");
    }

    /**导出客户资源**/
    @Override
    public List<BigEmp> exportBigEmpInfo(String exportids,MyUser user) {
        String []exportsIds  = exportids.split(",");
        List<String> ids = new ArrayList<>();
        for(String id:exportsIds){
            ids.add(id);
        }
        Example example = new Example(BigEmp.class);
        example.createCriteria().andIn("bpId",ids);
        List<BigEmp> emps =  this.selectByExample(example);
        emps.forEach( emp ->{ AutonCusInfo.parseBigEmp(emp,user); });
        return emps;
    }

    /**导出客户资源的记录**/
    @Override
    @Transactional
    public void exportBigEmpRecords(BigEmp bigEmp,String exportids,String MyuserInfo,String msg) throws Exception {
        try {
            /**筛选条件**/
            JSONObject json  = new JSONObject(true);
            IpRecords ipRecords = new IpRecords();
            /**查看大名单批次是不是为空**/
            String bpBatch = bigEmp.getBpBatch();
            if (!StringUtils.isEmpty(bpBatch)){
                ipRecords.setcIpEname(bpBatch);
            }
            //筛选条件大资源批次
            json.put("B",bpBatch);
            json.put("D",bigEmp.getBpDirector());
            /**导出的数量**/
            String []exportsIds  = exportids.split(",");
            ipRecords.setcIpNum(exportsIds.length);
            List<String> idsall = new ArrayList<>();
            for(String id:exportsIds){
                idsall.add(id);
            }
            ipRecords.setcIpIds(exportids);
            /**查询的客户的人员**/
            //高校筛选
            String collage = bigEmp.getBpColleges();
            if (!StringUtils.isEmpty(collage)){
                json.put("U",collage);
                //学院
                String bpAcademy = bigEmp.getBpAcademy();
                if (!StringUtils.isEmpty(bpAcademy)){
                    json.put("A",bpAcademy);
                    //专业
                    String bpMajor = bigEmp.getBpMajor();
                    if (!StringUtils.isEmpty(bpMajor)){
                        json.put("M",bpMajor);
                    }else{
                        json.put("M","");
                    }
                }else{
                    json.put("A","");
                }
            }else{
                json.put("U","");
                json.put("A","");
                json.put("M","");
            }
            //客户意向筛选
            String bpGroud = bigEmp.getBpGroud();
            json.put("G",bpGroud);
            //导出次数
            String bpEpNum = bigEmp.getBpEpNum();
            json.put("N",bpEpNum);
            //大资源类型
            String bpResType  = bigEmp.getBpResType();
            if (Constant.BIGEMP_COLLAGE_CODE.equals(bpResType)){
                //高校学生资源
                json.put("Y",Constant.BIGEMP_COLLAGE);
            }else if (Constant.BIGEMP_TONGHANG_CODE.equals(bpResType)){
                //同行机构资源
                json.put("Y",Constant.BIGEMP_TONGHANG);
            }else if (Constant.BIGEMP_ZHAOPIN_CODE.equals(bpResType)){
                //招聘平台资源
                json.put("Y",Constant.BIGEMP_ZHAOPIN);
            }else if (Constant.BIGEMP_QITA_CODE.equals(bpResType)) {
                //其他渠道资源
                json.put("Y",Constant.BIGEMP_QITA);
            }else{
                json.put("Y","");
            }
            json.put("YC",bpResType);
            //初始意向
            String bpInteProject = bigEmp.getBpInteProject();
            json.put("P",bpInteProject);
            //来源渠道
            String  bpSOurChanel = bigEmp.getBpSourChanel();
            json.put("S",bpSOurChanel);
            //客户新增时间
            String starttime = bigEmp.getStarttime();
            json.put("ST",starttime);
            String endtime = bigEmp.getEndtime();
            json.put("ET",endtime);
            //用户状态 已经分配 未分配
            String bpMyCustomer = bigEmp.getBpMyCustomer();
            if (!StringUtils.isEmpty(bpMyCustomer)){
                if ("distrut".equals(bpMyCustomer)){
                    json.put("F",true);
                }else{
                    json.put("F",false);
                }
            }else{
                json.put("F","");
            }
            ipRecords.setcIpIcomdtion(json.toJSONString());

            /**导出大资源名单的 批次**/
            String exportname = Utils.getExportBatchName(idsall.size()+"");
            ipRecords.setcIpIname(exportname);
            ipRecords.setcIpDirector(bigEmp.getBpDirector());
            String uuid = Utils.getUUID();
            ipRecords.setcIpUuid(uuid);
            ipRecords.setcIpTime(new Date());
            ipRecords.setcIpOption(MyuserInfo);
            ipRecords.setcIpStatus("0");
            ipRecords.setcIpIsover("0");
            ipRecords.setcIpUserfo(msg);
            ipRecords.setcIpType(Constant.ZIYUAN_TYPE_SCHOLLE);
            ipRecordsMapper.insert(ipRecords);
        }catch (Exception e){
            throw  new RuntimeException("增加导出记录的时候失败："+e.getMessage());
        }
    }

    @Override
    public List<String> findAllBigEmp() {
         List<String> listNum = new ArrayList<>();
        try {
            listNum = this.bigEmpMapper.getAllphone();
        }catch (Exception e){
            log.error(""+e.getMessage());
        }
        return listNum;
    }
}
